<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>ECharts LineMarker with OpenLayers</title>
    <script type="text/javascript" include="echarts" src="../../dist/include-openlayers.js"></script>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%">
<div id="map" class='map' style="margin:0 auto;width: 100%;height: 100%;border: 1px solid #dddddd">
</div>
<div id="infoControl"
     style=" position: absolute;top: 10px;right: 5px;text-align: left;background: white;color: #333;box-shadow: 0 3px 14px rgba(0,0,0,0.4);"></div>
<script type="text/javascript">
    var resultLayer;
    var map = new ol.Map({
        target: 'map',
        controls: ol.control.defaults({attributionOptions: {collapsed: false}})
            .extend([new ol.supermap.control.Logo()]),
        view: new ol.View({
            center: [116.402, 39.905],
            zoom: 5,
            projection: 'EPSG:4326'
        }),
        layers: [new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest({
                url: (window.isLocal ? document.location.protocol + "//" + document.location.host : "http://support.supermap.com.cn:8090")+"/iserver/services/map-world/rest/maps/世界地图_Gray",
            }),
            projection: 'EPSG:4326'
        })]
    });
    var option = {
        title: {
            text: '北京市未来一周气温变化',
            textStyle: {
                color: '#404a59',
                fontSize: 16
            },
            subtext: '纯属虚构'
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: ['最高气温', '最低气温']
        },
        toolbox: {
            show: true,
            feature: {
                dataZoom: {
                    yAxisIndex: 'none'
                },
                magicType: {type: ['line', 'bar']},
                restore: {},
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
            type: 'value',
            axisLabel: {
                formatter: '{value} °C'
            }
        },
        series: [
            {
                name: '最高气温',
                type: 'line',
                data: [11, 11, 15, 13, 12, 13, 10],
                markPoint: {
                    data: [
                        {type: 'max', name: '最大值'},
                        {type: 'min', name: '最小值'}
                    ]
                },
                markLine: {
                    data: [
                        {type: 'average', name: '平均值'},
                        [{
                            symbol: 'none',
                            x: '90%',
                            yAxis: 'max'
                        }, {
                            symbol: 'circle',
                            label: {
                                normal: {
                                    position: 'start',
                                    formatter: '最大值'
                                }
                            },
                            type: 'max',
                            name: '最高点'
                        }]
                    ]
                }
            },
            {
                name: '最低气温',
                type: 'line',
                data: [1, -2, 2, 5, 3, 2, 0],
                markPoint: {
                    data: [
                        {type: 'max', name: '最大值'},
                        {type: 'min', name: '最小值'}
                    ]
                },
                markLine: {
                    data: [
                        {type: 'average', name: '平均值'},
                        [{
                            symbol: 'none',
                            x: '90%',
                            yAxis: 'min'
                        }, {
                            symbol: 'circle',
                            label: {
                                normal: {
                                    position: 'start',
                                    formatter: '最小值'
                                }
                            },
                            type: 'min',
                            name: '最低点'
                        }]
                    ]
                }
            }
        ]
    };
    var chart = echarts.init(document.createElement('div'), '', {
        width: 600,
        height: 400
    });
    chart.setOption(option);
    var control = new ol.control.Control({
        element: chart.getDom(),
        target: document.getElementById('infoControl')
    });
    map.addControl(control);
    query();
    function query() {
        clearLayer();
        var queryService = new ol.supermap.QueryService("http://support.supermap.com:8090/iserver/services/map-china400/rest/maps/China");
        var param = new SuperMap.QueryBySQLParameters({
            queryParams: [{
                name: "China_ProCenCity_pt@China",
                attributeFilter: "NAME = '广州市' or NAME = '上海市'"
            }, {
                name: "China_Capital_pt@China",
                attributeFilter: "NAME = '北京市'"
            }]
        });
        queryService.queryBySQL(param, function (serviceResult) {
            var features = [];
            for (var i = 0; i < serviceResult.result.recordsets.length; i++) {
                var temp = (new ol.format.GeoJSON()).readFeatures(serviceResult.result.recordsets[i].features, {
                    dataProjection: 'EPSG:3857',
                    featureProjection: 'EPSG:4326'
                });
                features = features.concat(temp);
            }
            resultLayer = new ol.layer.Vector({
                source: new ol.source.Vector({
                    wrapX: false,
                    features: features
                })
            });
            map.addLayer(resultLayer);
            var select = new ol.interaction.Select({features: [features[2]]});
            map.addInteraction(select);
            select.on('select', function (e) {
                if (this.getFeatures().getLength() > 0) {
                    var city = this.getFeatures().item(0).getProperties().NAME;
                    var data1 = [];
                    var data2 = [];
                    for (var i = 0; i < 7; i++) {
                        var data = Math.random().toFixed(2);
                        data1.push(data);
                        data2.push(data * (Math.random() + 1.5));
                    }
                    chart.setOption({
                        title: {
                            text: city + '未来一周气温变化',
                        },
                        series: [
                            {
                                name: '最高气温',
                                data: [10 + Math.ceil(Math.random() * 10), 10 + Math.ceil(Math.random() * 10), 10 + Math.ceil(Math.random() * 10), 10 + Math.ceil(Math.random() * 10), 10 + Math.ceil(Math.random() * 10), 10 + Math.ceil(Math.random() * 10), 10 + Math.ceil(Math.random() * 10)],
                            },
                            {
                                name: '最低气温',
                                data: [8 - Math.ceil(Math.random() * 10), 8 - Math.ceil(Math.random() * 10), 8 - Math.ceil(Math.random() * 10), 8 - Math.ceil(Math.random() * 10), 8 - Math.ceil(Math.random() * 10), 8 - Math.ceil(Math.random() * 10), 8 - Math.ceil(Math.random() * 10)],

                            }
                        ]
                    });
                }
                map.getView().setCenter(e.mapBrowserEvent.coordinate);
            });
        });
    }
    function clearLayer() {
        if (resultLayer) {
            map.removeLayer(resultLayer);
        }
    }
</script>
</body>
</html>